//
//  WDLoger.swift
//  Woody
//
//  Created by Alean on 2022/11/30.
//

import UIKit
import XCGLogger
/**
Swift 使用方式
 WDLog("hhhhh", .error) 默认 level 为 .debug

OC 使用方式
 WDLoginfo(@"info = %@", @"dddd");
 WDLogerror(@"error = %@", @"dddd");
 WDLogsevere(@"severe = %@", @"dddd");
 WDLogverbose(@"verbose = %@", @"dddd");
 WDLogwarning(@"warning = %@", @"dddd");
 WDLogdebug(@"debug = %@", @"dddd");
 */

public let log = XCGLogger.default
public func WDLog(_ closure: @autoclosure () -> Any?,
                  _ level: XCGLogger.Level = .debug,
                  _ functionName: StaticString = #function,
                  _ fileName: StaticString = #file,
                  _ lineNumber: Int = #line,
                  userInfo: [String: Any] = [:]) {
    log.logln(level, functionName: functionName, fileName: fileName, lineNumber: lineNumber, userInfo: userInfo, closure: closure)
}

public class WDLoger: NSObject {
    @objc public class func setupLogger() {
        log.setup(level: .verbose)
        do {
            let path = try WDHelper.cacheDirectory(type: .logFile)
            let fileDestination = AutoRotatingFileDestination(writeToFile: path + "com.woody.sh.log",
                                                              identifier: "com.woody.sh",
                                                              shouldAppend: true,
                                                              maxFileSize: 2 * 1024 * 1024,
                                                              maxTimeInterval: 60 * 60 * 24 * 7)
            fileDestination.targetMaxLogFiles = 10
            fileDestination.outputLevel = .verbose
            fileDestination.showFunctionName = true
            fileDestination.showFileName = true
            fileDestination.showDate = true
            fileDestination.logQueue = XCGLogger.logQueue
            log.add(destination: fileDestination)
        } catch let error {
            WDLog(error.localizedDescription, .error)
        }
    }

    public class func wdlog(_ string: String,
                     _ level: XCGLogger.Level = .debug,
                     _ functionName: UnsafePointer<CChar>,
                     _ fileName: UnsafePointer<CChar>,
                     _ lineNumber: Int = #line,
                     _ userInfo: [String: Any] = [:],
                     _ closure: () -> Any?) {
        let funname = String(cString: functionName, encoding: .ascii) ?? ""
        let filename = String(cString: fileName, encoding: .ascii) ?? ""
        log.logln(level, functionName: funname, fileName: filename, lineNumber: lineNumber, userInfo: userInfo, closure: closure)
    }
    
    @objc public static func verbose(_ string: String, functionName: UnsafePointer<CChar>, fileName: UnsafePointer<CChar>, lineNumber: Int) {
        wdlog(string, .verbose, functionName, fileName, lineNumber, [:], { return string })
    }

    @objc public static func debug(_ string: String, functionName: UnsafePointer<CChar>, fileName: UnsafePointer<CChar>, lineNumber: Int) {
        wdlog(string, .debug, functionName, fileName, lineNumber, [:], { return string })
    }

    @objc public static func info(_ string: String, functionName: UnsafePointer<CChar>, fileName: UnsafePointer<CChar>, lineNumber: Int) {
        wdlog(string, .info, functionName, fileName, lineNumber, [:], { return string })
    }
    
    @objc public static func warning(_ string: String, functionName: UnsafePointer<CChar>, fileName: UnsafePointer<CChar>, lineNumber: Int) {
        wdlog(string, .warning, functionName, fileName, lineNumber, [:], { return string })
    }
    
    @objc public static func error(_ string: String, functionName: UnsafePointer<CChar>, fileName: UnsafePointer<CChar>, lineNumber: Int) {
        wdlog(string, .error, functionName, fileName, lineNumber, [:], { return string })
    }
    
    @objc public static func severe(_ string: String, functionName: UnsafePointer<CChar>, fileName: UnsafePointer<CChar>, lineNumber: Int) {
        wdlog(string, .severe, functionName, fileName, lineNumber, [:], { return string })
    }

    @objc public static func test() {
        verbose("verbose级别消息：程序执行时最详细的信息。", functionName: #function, fileName: #file, lineNumber: #line)
        debug("debug级别消息：用于代码调试。", functionName: #function, fileName: #file, lineNumber: #line)
        info("info级别消息：常用与用户在console.app中查看。", functionName: #function, fileName: #file, lineNumber: #line)
        warning("warning级别消息：警告消息，表示一个可能的错误。", functionName: #function, fileName: #file, lineNumber: #line)
        error("error级别消息：表示产生了一个可恢复的错误，用于告知发生了什么事情。", functionName: #function, fileName: #file, lineNumber: #line)
        severe("severe error级别消息：表示产生了一个严重错误。程序可能很快会奔溃。", functionName: #function, fileName: #file, lineNumber: #line)
    }
}
